package com.car.serve.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.commons.core.model.Result;
import com.car.commons.core.util.JwtUtil;
import com.car.commons.web.util.WebUtil;
import com.car.serve.Param.addServeParam;
import com.car.serve.Param.reviseServeParam;
import com.car.serve.dto.ServeDto;
import com.car.serve.dto.ServeInfoClientDto;
import com.car.serve.dto.ServeInfoDto;
import com.car.serve.entity.ServeInfo;
import com.car.serve.exception.ServeException;
import com.car.serve.form.PageServeForm;
import com.car.serve.form.addServeForm;
import com.car.serve.form.reviseServeForm;
import com.car.serve.service.ServeInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@RestController
@RequestMapping("/serveInfo")
@Api(tags = "服务信息")
public class ServeInfoController {

    @Resource
    private ServeInfoService serveInfoService;

    //通过ID获取服务client接口
    @GetMapping("/serveById.ty")
    @ApiOperation(value = "通过ID获取服务client接口")
    public Result<ServeInfoClientDto> serveById(Integer serveId)throws Exception{
        ServeInfo serveInfo = serveInfoService.getById(serveId);
        if (serveInfo==null)throw new ServeException(3000,"该服务不存在");
        if (serveInfo.getServeState()!=0)throw new ServeException(3000,"状态异常");
        return Result.getResult(ServeInfoClientDto.class).setMsg("通过ID查询服务成功").setData(BeanUtil.toBean(serveInfo, ServeInfoClientDto.class));
    }
    //通过ID查询服务详情
    @GetMapping("/findById.pt")
    @ApiOperation(value = "通过ID查询服务详情")
    public Result<ServeInfoDto>findById(Integer serveId)throws Exception{
        ServeInfoDto byId = serveInfoService.findById(serveId);
        return Result.getResult(ServeInfoDto.class).setMsg("查询详情成功").setData(byId);
    }
    //新增服务
    @PostMapping("/addServe.pt")
    @ApiOperation(value = "新增服务")
    public Result addServe(@RequestBody addServeForm form)throws Exception{
        serveInfoService.addServe(BeanUtil.toBean(form,addServeParam.class));
        return Result.getResult().setMsg("新增服务成功");
    }
    //修改服务内容
    @PostMapping("/reviseServe.pt")
    @ApiOperation(value = "修改服务内容")
    public Result reviseServe(@RequestBody reviseServeForm form)throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        reviseServeParam param = BeanUtil.toBean(form, reviseServeParam.class);
        param.setAdminId(adminId);
        serveInfoService.reviseServe(param);
        return Result.getResult().setMsg("修改服务内容成功");
    }

    //修改服务状态
    @PostMapping("/disableServe.pt")
    @ApiOperation(value = "禁用服务")
    public Result disableServe(Integer serveId)throws Exception{
        serveInfoService.disableServe(serveId);
        return Result.getResult().setMsg("禁用服务成功");
    }

    @PostMapping("/enableServe.pt")
    @ApiOperation(value = "启用服务")
    public Result enableServe(Integer serveId)throws Exception{
        serveInfoService.enableServe(serveId);
        return Result.getResult().setMsg("启用服务成功");
    }

    @PostMapping("/delServe.pt")
    @ApiOperation(value = "删除服务")
    public Result delServe(Integer serveId)throws Exception{
        serveInfoService.delServe(serveId);
        return Result.getResult().setMsg("删除服务成功");
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/findServePage.pt")
    public Result findServePage(@RequestBody PageServeForm form) {
        Page<ServeInfo> servePage = serveInfoService.findServePage(form);
        return Result.getResult().setMsg("分页查询成功").setData(servePage);
    }
    /**
     * 修改服务评分
     *
     * @return
     */
    @PostMapping("/serveInfo/changeServeScore.ty")
    @ApiOperation(value = "修改评分")
    public Result changeServeScore(@RequestBody ServeDto serveDto)throws Exception{
        serveInfoService.changeServeScore(serveDto);
        return Result.getResult().setMsg("修改评分成功");
    }




}

